package com.example.fishdriver.recyclerviewdemo.updatestyleloadAndrefresing;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.example.fishdriver.recyclerviewdemo.R;
import com.example.fishdriver.recyclerviewdemo.TestAdapter;
import com.lhh.ptrrv.library.CustomSwipeRefreshLayout;
import com.lhh.ptrrv.library.PullToRefreshRecyclerViewCustom;

import java.util.ArrayList;
import java.util.List;

/**
 * 加载和下拉
 */
public class LoadAndRefreshingActivity extends AppCompatActivity {
    private PullToRefreshRecyclerViewCustom mPullToRefreshRecyclerViewCustom;
    private List<String> list;

    private TestAdapter testAdapter;
    private static final int DEFAULT_ITEM_SIZE = 20;
    private static final int ITEM_SIZE_OFFSET = 20;

    private static final int MSG_CODE_REFRESH = 0;
    private static final int MSG_CODE_LOADMORE = 1;

    private static final int TIME = 5000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_and_refreshing);
        list = new ArrayList<>();
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        list.add("啥课代表暗示暗示了贷款");
        mPullToRefreshRecyclerViewCustom = (PullToRefreshRecyclerViewCustom) findViewById(R.id.ptrrv);
        mPullToRefreshRecyclerViewCustom.setAdapter(testAdapter = new TestAdapter(this, R.layout.item_de, list));
        mPullToRefreshRecyclerViewCustom.setLayoutManager(new LinearLayoutManager(this));
        mPullToRefreshRecyclerViewCustom.setPagingableListener(new PullToRefreshRecyclerViewCustom.PagingableListener() {
            @Override
            public void onLoadMoreItems() {
                mPullToRefreshRecyclerViewCustom.showLoadView(false);
                mHandler.sendEmptyMessageDelayed(MSG_CODE_LOADMORE, TIME);
            }
        });
        mPullToRefreshRecyclerViewCustom.setSwipeEnable(true);
        mPullToRefreshRecyclerViewCustom.setOnRefreshListener(new CustomSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.sendEmptyMessageDelayed(MSG_CODE_REFRESH, TIME);
            }
        });
        mPullToRefreshRecyclerViewCustom.getRecyclerView().addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
        DemoLoadMoreView loadMoreView = new DemoLoadMoreView(this, mPullToRefreshRecyclerViewCustom.getRecyclerView());
        loadMoreView.setLoadmoreString("加载中....");
        loadMoreView.setLoadMorePadding(100);
        mPullToRefreshRecyclerViewCustom.setLoadMoreFooter(loadMoreView);
//        mPullToRefreshRecyclerViewCustom.showLoadView(false);
//        mHandler.sendEmptyMessageDelayed(MSG_CODE_LOADMORE, TIME);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MSG_CODE_REFRESH) {
                list.clear();
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                list.add("啥课代表暗示暗示了贷款");
                mPullToRefreshRecyclerViewCustom.setOnRefreshComplete();
//                mPullToRefreshRecyclerViewCustom.showLoadView(false);
                testAdapter.notifyDataSetChanged();
            } else if (msg.what == MSG_CODE_LOADMORE) {
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                list.add("加载的数据。。。。。");
                mPullToRefreshRecyclerViewCustom.hideShowView();
                testAdapter.notifyDataSetChanged();
            }
        }
    };
}
